package com.venue.hjt.venueclass.service;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Environment;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import com.google.gson.Gson;
import com.school.hjt.schoolclasscard.IMyAidlInterface;
import com.venue.hjt.venueclass.BaseApplication;
import com.venue.hjt.venueclass.bean.API;
import com.venue.hjt.venueclass.bean.HomeBean;
import com.venue.hjt.venueclass.bean.VenueBean;
import com.venue.hjt.venueclass.bean.VenueBookingBean;
import com.venue.hjt.venueclass.db.AddData;
import com.venue.hjt.venueclass.db.CardDao;
import com.venue.hjt.venueclass.db.CardStudent;
import com.venue.hjt.venueclass.db.DynamicSchool;
import com.venue.hjt.venueclass.db.SchAboutUs;
import com.venue.hjt.venueclass.db.SchAlbum;
import com.venue.hjt.venueclass.db.SchArticle;
import com.venue.hjt.venueclass.db.VenueBooKing;
import com.venue.hjt.venueclass.db.VenueEvaluate;
import com.venue.hjt.venueclass.db.VenueGroup;
import com.venue.hjt.venueclass.db.VenueHour;
import com.venue.hjt.venueclass.db.VenueInfo;
import com.venue.hjt.venueclass.db.VenueKeyword;
import com.venue.hjt.venueclass.utlis.APPUtlis;
import com.venue.hjt.venueclass.utlis.ClassCalendar;
import com.venue.hjt.venueclass.utlis.Constant;
import com.venue.hjt.venueclass.utlis.DownLoadImageService;
import com.venue.hjt.venueclass.utlis.ImageDownLoadCallBack;
import com.venue.hjt.venueclass.utlis.OkhttPost;
import com.venue.hjt.venueclass.utlis.PrefUtild;


import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import okhttp3.Call;

/**
 * Created by xqx on 2017/11/10.
 * 更新服务
 */
public class LocalService extends Service  {
    private ServiceConnection conn;
    private MyService myService;
    Intent intent = new Intent();
    List<String> studentList=new ArrayList<>();//删除学生
    List<String> AlbumId=new ArrayList<>();//删除相册
    List<String> KeywordsId=new ArrayList<>();//删除动态
    List<String> ArtSchoolId=new ArrayList<>();//删除动态
    List<String> Sch_article=new ArrayList<>();//删除值日生
    List<String> Sch_AboutUs=new ArrayList<>();//删除值日生
    List<String> VenueGroupId=new ArrayList<>();//删除值日生
    List<String> BookingId=new ArrayList<>();//删除值日生
    List<String> VenueEvaluateId=new ArrayList<>();//删除值日生
    AddData addData=new AddData();
    @Override
    public IBinder onBind(Intent intent) {
        return myService;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        init();
        AlarmManager();
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run(){
                String class_no = PrefUtild.INSTANCE.getString(getApplicationContext(), "class_no", "");
                Log.e("ATG",class_no);
                if (!class_no .equals("")) {
                    OkhttPost.Okget(API.HOME + Constant.SNO, 1, new ServiceClass());
                    OkhttPost.Okget(API.VENUE + Constant.SNO+"/"+class_no, 2, new ServiceClass());
                    OkhttPost.Okget(API.VENUEBOOKINGLIST + Constant.SNO+"/"+class_no, 3, new ServiceClass());
                }
            }
        },10000,22000);
    }
    /**
     //     * 单线程列队执行
     //     */
    private static ExecutorService singleExecutor = null;
    /**
     * 执行单线程列队执行
     */
    public void runOnQueue(Runnable runnable) {
        if (singleExecutor == null) {
            singleExecutor = Executors.newSingleThreadExecutor();
        }
        singleExecutor.submit(runnable);
    }
    /**
     * 启动图片下载线程
     */
    private void onDownLoad(final String url, final String type) {
        DownLoadImageService service = new DownLoadImageService(getApplicationContext(),
                url,
                new ImageDownLoadCallBack() {

                    @Override
                    public void onDownLoadSuccess(File file) {
                        // 在这里执行图片保存方法
                        APPUtlis.copyFile(file.getAbsolutePath(), Environment.getExternalStorageDirectory().getPath() + "/Face/"+APPUtlis.getMd5Value(url)+".hjt");
                        if (type.equals("1")){
                            intent.setAction(Constant.BANNER);
                            sendBroadcast(intent);
                        }else{
                            intent.setAction(Constant.RONGYU);
                            sendBroadcast(intent);
                        }
                    }
                    @Override
                    public void onDownLoadFailed() {
                        // 图片保存失败
                    }
                });
        //启动图片下载线程
        runOnQueue(service);
    }
    private void init() {
        if (conn == null) {
            conn = new MyServiceConnection();
        }
        myService = new MyService();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Intent intents = new Intent();
        intents.setClass(this, RemoteService.class);
        bindService(intents, conn, Context.BIND_IMPORTANT);
        return START_STICKY;
    }
    class MyService extends IMyAidlInterface.Stub {
        @Override
        public String getName() throws RemoteException {
            return null;
        }
    }
    private class MyServiceConnection implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            System.out.println("获取连接");
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            LocalService.this.startService(new Intent(LocalService.this,
                    RemoteService.class));
            LocalService.this.bindService(new Intent(LocalService.this,
                    RemoteService.class), conn, Context.BIND_IMPORTANT);
        }

    }
    public void AlarmManager(){
        AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
        long triggerAtTime = System.currentTimeMillis();
        Intent intent = new Intent();
//        intent.setAction(Constant.TO);
        sendBroadcast(intent);
        PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, 0);
        manager.setRepeating(AlarmManager.RTC_WAKEUP, triggerAtTime,AlarmManager.INTERVAL_HALF_HOUR , pi);
//        manager.set(AlarmManager.RTC_WAKEUP,triggerAtTime,pi);
    }
    class ServiceClass implements OkhttPost.Callback{
        @Override
        public void onError(Call call, Exception e, int id) {
            intent.setAction(Constant.ERR);
            intent.putExtra(Constant.ERR, ClassCalendar.getIntDay()+": 主页数据接口访问失败"+e.toString());
            sendBroadcast(intent);

        }
        @Override
        public void onResponse(String response, int id) {
            Gson gson = new Gson();
            try {
                if (id==1){
//                    intent.setAction(Constant.ERR);
//                    intent.putExtra(Constant.ERR, ClassCalendar.getStringtHour()+": 主页接口访问成功");
//                    sendBroadcast(intent);
                    HomeBean homeBean = gson.fromJson(response, HomeBean.class);
                        ArtSchoolId.clear();
                        List<HomeBean.Art_school> art_schools = homeBean.art_school;
                        for (HomeBean.Art_school art_school:art_schools){
                            ArtSchoolId.add(art_school.id);
                            List<DynamicSchool> dynamicSchools = CardDao.queryDynamicSchoolId(art_school.id);
                            if (dynamicSchools.size()!=0){
                                if (!dynamicSchools.get(0).add_time.equals(art_school.add_time)){
//                                    Log.e("ATG","1");
                                    addData.addDynamicSchool(art_school.id,art_school.content,art_school.add_time);
                                    intent.setAction(Constant.ARTSCHOOL);
                                    sendBroadcast(intent);
                                }
                            }else{
//                                Log.e("ATG","2");
                                    addData.addDynamicSchool(art_school.id,art_school.content,art_school.add_time);
                                    intent.setAction(Constant.ARTSCHOOL);
                                    sendBroadcast(intent);
                            }
                        }
                        AlbumId.clear();
                        List<HomeBean.Album> album = homeBean.album;
                        for (int i=0;i<album.size();i++){
                            AlbumId.add(album.get(i).id);
                            List<SchAlbum> classAlba = CardDao.queryAlbumId(album.get(i).id);
                            if (classAlba.size()!=0){
                                String str = album.get(i).title == null ? "无标题" : album.get(i).title;
                                if (!classAlba.get(0).getTitle().equals(str)){
//                                    Log.e("ATG","3");
                                    addData.addAlbum(album.get(i).id,album.get(i).add_time,album.get(i).pic_path,album.get(i).type_id,album.get(i).title==null?"无标题":album.get(i).title);
                                    if (album.get(i).type_id.equals("1")){
                                        intent.setAction(Constant.ALBUM);
                                        sendBroadcast(intent);
                                        intent.setAction(Constant.BANNER);
                                        sendBroadcast(intent);
                                    }else {
                                        intent.setAction(Constant.RONGYU);
                                        sendBroadcast(intent);
                                    }
                                }
                            }else{
//                                Log.e("ATG","4");
                                    addData.addAlbum(album.get(i).id,album.get(i).add_time,album.get(i).pic_path,album.get(i).type_id,album.get(i).title==null?"无标题":album.get(i).title);
                                if (album.get(i).type_id.equals("1")){
                                    intent.setAction(Constant.ALBUM);
                                    sendBroadcast(intent);
                                }
                                    onDownLoad(album.get(i).pic_path,album.get(i).type_id);
                            }
                        }
                            studentList.clear();
                            List<HomeBean.Student> students = homeBean.student;
                            for (int i=0;i<students.size();i++){
                                studentList.add(students.get(i).student_no);
                                    String headimg="";
                                    if (students.get(i).headimg!=null){
                                        headimg=students.get(i).headimg;
                                    }
                                    List<CardStudent> cardStudents = CardDao.queryStudentId(students.get(i).student_no);
                                    if (cardStudents.size()!=0){
                                        if (!cardStudents.get(0).getCard_no().equals(students.get(i).card_no==null?students.get(i).student_no:students.get(i).card_no)||!cardStudents.get(0).getStudent_name().equals(students.get(i).name)
                                                || !cardStudents.get(0).getStudent_url().equals(headimg)|| !cardStudents.get(0).getScore().equals(students.get(i).score==null?"0":students.get(i).score)){
//                                        Log.e("ATG","进来学生");
//                                            Log.e("ATG","5");
                                            addData.addStudent(students.get(i).student_no,students.get(i).card_no==null?students.get(i).student_no:students.get(i).card_no,students.get(i).name,headimg,students.get(i).score==null?"0":students.get(i).score,1);
//                                            intent.setAction(Constant.KQ);
//                                            sendBroadcast(intent);

                                        }
                                    }else{
//                                        Log.e("ATG","进来学生1"+students.get(i).name);
                                            addData.addStudent(students.get(i).student_no,students.get(i).card_no==null?students.get(i).student_no:students.get(i).card_no,students.get(i).name,headimg,students.get(i).score==null?"0":students.get(i).score,1);
//                                        intent.setAction(Constant.KQ);
//                                        sendBroadcast(intent);
                                    }

                            }
                        List<HomeBean.Sch_article> sch_article = homeBean.sch_article;
                        Sch_article.clear();
                        for (int i=0;i<sch_article.size();i++){
                            Sch_article.add(sch_article.get(i).id);
                            List<SchArticle> schArticles = CardDao.querySchArticle(sch_article.get(i).id);
                            if (schArticles.size()!=0){
                                if (!schArticles.get(0).add_time.equals(sch_article.get(i).add_time)){
                                    Log.e("ATG","9");
                                    addData.addSchArtcle(sch_article.get(i).id,sch_article.get(i).title,sch_article.get(i).sort,
                                            sch_article.get(i).image_path,sch_article.get(i).content,sch_article.get(i).link,sch_article.get(i).add_time);
                                    intent.setAction(Constant.ARTICLR);
                                    sendBroadcast(intent);
                                }
                            }else{
                                    Log.e("ATG","10");
                                    addData.addSchArtcle(sch_article.get(i).id,sch_article.get(i).title,sch_article.get(i).sort,
                                            sch_article.get(i).image_path,sch_article.get(i).content,sch_article.get(i).link,sch_article.get(i).add_time);
                                    intent.setAction(Constant.ARTICLR);
                                    sendBroadcast(intent);
                            }
                        }
                        List<HomeBean.About_us> about_us = homeBean.about_us;
                        Sch_AboutUs.clear();
                        for (int i=0;i<about_us.size();i++){
                            Sch_AboutUs.add(about_us.get(i).id);
                            List<SchAboutUs> schAboutUses = CardDao.querySchAboutUs(about_us.get(i).id);
                            if (schAboutUses.size()!=0){
                                if (!schAboutUses.get(0).add_time.equals(about_us.get(i).add_time)){
                                    addData.addSchAboutUs(about_us.get(i).id,about_us.get(i).title,about_us.get(i).sort,about_us.get(i).type_id,about_us.get(i).
                                            content,about_us.get(i).link,about_us.get(i).add_time);
                                    intent.setAction(Constant.JIANJIE);
                                    sendBroadcast(intent);
                                }
                            }else{
                                    addData.addSchAboutUs(about_us.get(i).id,about_us.get(i).title,about_us.get(i).sort,about_us.get(i).type_id,about_us.get(i).
                                            content,about_us.get(i).link,about_us.get(i).add_time);
                                    intent.setAction(Constant.JIANJIE);
                                    sendBroadcast(intent);
                            }
                        }
                        //删除简介
                        List<SchAboutUs> schAboutUs = CardDao.querySchAboutUs();
                        for (SchAboutUs aboutUs:schAboutUs){
                            boolean contains = Sch_AboutUs.contains(aboutUs.getId());
                            if (!contains){
                                BaseApplication.getDaoInstant().getSchAboutUsDao().deleteByKey(aboutUs.getId());
                                BaseApplication.getDaoInstant().clear();
//                                Log.e("ATG","3   3");
                                intent.setAction(Constant.JIANJIE);
                                sendBroadcast(intent);
//                            OkhttPost.FaceDel(getApplicationContext(),student.getCard_no(),class_no,new ServiceClaa());
                            }
                        }
                        //删除资讯
                        List<SchArticle> schArticles = CardDao.querySchArticle();
                        for (SchArticle schArticle:schArticles){
                            boolean contains = Sch_article.contains(schArticle.getId());
                            if (!contains){
                                BaseApplication.getDaoInstant().getSchArticleDao().deleteByKey(schArticle.getId());
                                BaseApplication.getDaoInstant().clear();
                                intent.setAction(Constant.ARTICLR);
                                sendBroadcast(intent);
//                            OkhttPost.FaceDel(getApplicationContext(),student.getCard_no(),class_no,new ServiceClaa());
                            }
                        }
//                        //删除学生
//                        List<CardStudent> cardStudents = CardDao.queryStudent();
//                        for (CardStudent studentd:cardStudents){
//                            boolean contains = studentList.contains(studentd.getId());
//                            if (!contains){
//                                BaseApplication.getDaoInstant().getCardStudentDao().deleteByKey(studentd.getId());
//                                BaseApplication.getDaoInstant().clear();
////                            OkhttPost.FaceDel(getApplicationContext(),student.getCard_no(),class_no,new ServiceClaa());
//                            }
//                        }
                        //删除相册
                        List<SchAlbum> classAlba = CardDao.queryAlbum();
                        for (SchAlbum album1:classAlba){
                            boolean contains = AlbumId.contains(album1.getId());
                            if (!contains){
                                BaseApplication.getDaoInstant().getSchAlbumDao().deleteByKey(album1.getId());
                                BaseApplication.getDaoInstant().clear();
                                Log.e("ATG","执行删除");
                                File file=new File(Environment.getExternalStorageDirectory().getPath() + "/Face/"+APPUtlis.getMd5Value(album1.getPic())+".hjt");
                                file.delete();
                                if (album1.getTypeid().equals("2")){
                                    intent.setAction(Constant.RONGYU);
                                    sendBroadcast(intent);
                                }else{
                                    intent.setAction(Constant.BANNER);
                                    sendBroadcast(intent);
                                    intent.setAction(Constant.ALBUM);
                                    sendBroadcast(intent);
                                }
                            }
                        }

                    }else if (id==2){
                    try {
                        VenueBean bean = gson.fromJson(response, VenueBean.class);
                        if (bean.code==1){
                            VenueBean.Data data = bean.data;
                            VenueBean.Settings settings = data.settings;
                            List<VenueBean.Manager> manager = settings.manager;
                            Log.e("ATG","1");
                            VenueEvaluateId.clear();
                            for (int i=0;i<manager.size();i++){
                                List<VenueEvaluate> venueEvaluates = CardDao.queryEvaluate(manager.get(i).id,0);
                                VenueEvaluateId.add(manager.get(i).id);
                                if (venueEvaluates.size()!=0){
                                    if (!venueEvaluates.get(0).getRemark().equals(manager.get(i).remark)||!venueEvaluates.get(0).getTitle().equals(manager.get(i).title)
                                            ||!venueEvaluates.get(0).getStar1().equals(manager.get(i).star1)||!venueEvaluates.get(0).getStar2().equals(manager.get(i).star2)
                                            ||!venueEvaluates.get(0).getStar3().equals(manager.get(i).star3)||!venueEvaluates.get(0).getStar4().equals(manager.get(i).star4)
                                            ||!venueEvaluates.get(0).getStar5().equals(manager.get(i).star5)){
                                        VenueEvaluate evaluate=new VenueEvaluate(manager.get(i).id,manager.get(i).title,manager.get(i).remark,manager.get(i).star1,manager.get(i).star2,
                                                manager.get(i).star3,manager.get(i).star4,manager.get(i).star5,"2",0);
                                        CardDao.insertEvaluate(evaluate);
                                    }
                                }else{
                                    VenueEvaluate evaluate=new VenueEvaluate(manager.get(i).id,manager.get(i).title,manager.get(i).remark,manager.get(i).star1,manager.get(i).star2,
                                            manager.get(i).star3,manager.get(i).star4,manager.get(i).star5,"2",0);
                                    CardDao.insertEvaluate(evaluate);
                                }
                            }
                            Log.e("ATG","2");
                            List<VenueBean.Applyer> applyer = settings.applyer;
                            for (int i=0;i<applyer.size();i++){
                                VenueEvaluateId.add(applyer.get(i).id);
//                                Log.e("ATG","201");
                                List<VenueEvaluate> venueEvaluates = CardDao.queryEvaluate(applyer.get(i).id,1);
                                if (venueEvaluates.size()!=0){
                                    if (!venueEvaluates.get(0).getRemark().equals(applyer.get(i).remark)||!venueEvaluates.get(0).getTitle().equals(applyer.get(i).title)
                                            ||!venueEvaluates.get(0).getStar1().equals(applyer.get(i).star1)||!venueEvaluates.get(0).getStar2().equals(applyer.get(i).star2)
                                            ||!venueEvaluates.get(0).getStar3().equals(applyer.get(i).star3)||!venueEvaluates.get(0).getStar4().equals(applyer.get(i).star4)
                                            ||!venueEvaluates.get(0).getStar5().equals(applyer.get(i).star5)){
                                        VenueEvaluate evaluate=new VenueEvaluate(applyer.get(i).id,applyer.get(i).title,applyer.get(i).remark,applyer.get(i).star1,applyer.get(i).star2,
                                                applyer.get(i).star3,applyer.get(i).star4,applyer.get(i).star5,"1",1);
                                        CardDao.insertEvaluate(evaluate);
                                    }
                                }else{
//                                    Log.e("ATG","202");
                                    VenueEvaluate evaluate=new VenueEvaluate(applyer.get(i).id,applyer.get(i).title,applyer.get(i).remark,applyer.get(i).star1,applyer.get(i).star2,
                                            applyer.get(i).star3,applyer.get(i).star4,applyer.get(i).star5,"1",1);
                                    CardDao.insertEvaluate(evaluate);
                                }
                            }
                            KeywordsId.clear();
                            Log.e("ATG","3");
                            List<VenueBean.Complain> complain = settings.complain;
                            for (int i=0;i<complain.size();i++){
                                KeywordsId.add(complain.get(i).id);
                                List<VenueKeyword> venueKeywords = CardDao.queryKey(complain.get(i).id, 0);
                                if (venueKeywords.size()!=0){
                                    if (!venueKeywords.get(0).getText().equals(complain.get(i).text)){
                                        VenueKeyword keyword=new VenueKeyword(complain.get(i).id,complain.get(i).text,0);//投诉
                                        CardDao.insertKeyword(keyword);
                                    }
                                }else{
                                    VenueKeyword keyword=new VenueKeyword(complain.get(i).id,complain.get(i).text,0);//投诉
                                    CardDao.insertKeyword(keyword);
                                }
                            }
                            Log.e("ATG","4");
                            List<VenueBean.Keyword> keywords = settings.keyword;
                            for (int i=0;i<keywords.size();i++){
                                KeywordsId.add(keywords.get(i).id);
                                List<VenueKeyword> venueKeywords = CardDao.queryKey(keywords.get(i).id, 1);
                                if (venueKeywords.size()!=0){
                                    if (!venueKeywords.get(0).getText().equals(keywords.get(i).text)){
                                        VenueKeyword keyword=new VenueKeyword(keywords.get(i).id,keywords.get(i).text,1);//预约
                                        CardDao.insertKeyword(keyword);
                                    }
                                }else{
                                    VenueKeyword keyword=new VenueKeyword(keywords.get(i).id,keywords.get(i).text,1);//预约
                                    CardDao.insertKeyword(keyword);
                                }
                            }
                            VenueBean.Venue_info venue_info = data.venue_info;
                            List<VenueInfo> venueInfos = CardDao.queryInfo(venue_info.id);
                            if (venueInfos.size()!=0){
                                if (!venueInfos.get(0).getVenue_name().equals(venue_info.venue_name)||!venueInfos.get(0).getDescription().equals(venue_info.description)
                                        ||!venueInfos.get(0).getMin_num().equals(venue_info.min_num)||!venueInfos.get(0).getMax_num().equals(venue_info.max_num)
                                        ||!venueInfos.get(0).getScore().equals(venue_info.score)||!venueInfos.get(0).getRegulation().equals(venue_info.regulation)||!venueInfos.get(0).getUser_score().equals(venue_info.user_score)){
                                    VenueInfo info=new VenueInfo(venue_info.id,venue_info.venue_name,venue_info.description,venue_info.min_num,venue_info.max_num,venue_info.score,venue_info.regulation,venue_info.add_time,venue_info.user_score);
                                    CardDao.insertInfo(info);
                                }
                            }else{
                                VenueInfo info=new VenueInfo(venue_info.id,venue_info.venue_name,venue_info.description,venue_info.min_num,venue_info.max_num,venue_info.score,venue_info.regulation,venue_info.add_time,venue_info.user_score);
                                CardDao.insertInfo(info);
                            }
                            Log.e("ATG","6");
                            List<VenueBean.Managers> managers = venue_info.managers;
                            for (int i=0;i<managers.size();i++){
                                List<CardStudent> cardStudents = CardDao.queryStudentId(managers.get(i).teacher_no + "t");
                                if (cardStudents.size()!=0){
                                    if (!cardStudents.get(0).getCard_no().equals(managers.get(i).card_no==null?managers.get(i).teacher_no:managers.get(i).card_no)){
                                        AddData.addStudent(managers.get(i).teacher_no+"t",managers.get(i).card_no==null?managers.get(i).teacher_no:managers.get(i).card_no,managers.get(i).teacher_name,"","",2);
                                    }
                                }else{
                                        AddData.addStudent(managers.get(i).teacher_no+"t",managers.get(i).card_no==null?managers.get(i).teacher_no:managers.get(i).card_no,managers.get(i).teacher_name,"","",2);
                                }
                            }
                            Log.e("ATG","7");
                            List<VenueBean.ClassHour> class_hour = venue_info.class_hour;
                            VenueGroupId.clear();
                            for (int i=0;i<class_hour.size();i++){
                                List<VenueHour> venueHours = CardDao.queryHourId(class_hour.get(i).id);
                                if (venueHours.size()!=0){
                                    if (!venueHours.get(0).getStart_time().equals(class_hour.get(i).start_time)||!venueHours.get(0).getEnd_time().equals(class_hour.get(i).end_time)||!venueHours.get(0).getStatus().equals(class_hour.get(i).status)){
                                        VenueHour hour=new VenueHour(class_hour.get(i).id,class_hour.get(i).weekday,class_hour.get(i).start_time,class_hour.get(i).end_time,class_hour.get(i).status);
                                        CardDao.insertHour(hour);
                                        intent.setAction(Constant.VENUEHOUR);
                                        sendBroadcast(intent);
                                    }
                                }else{
                                    VenueHour hour=new VenueHour(class_hour.get(i).id,class_hour.get(i).weekday,class_hour.get(i).start_time,class_hour.get(i).end_time,class_hour.get(i).status);
                                    CardDao.insertHour(hour);
                                    intent.setAction(Constant.VENUEHOUR);
                                    sendBroadcast(intent);
                                }
                                if (class_hour.get(i).status.equals("1")){
//                                Log.e("ATG","进来");
                                    List<VenueBean.Members> members = venue_info.class_hour.get(i).members;
                                    for (int j=0;j<members.size();j++){
                                        VenueGroupId.add(members.get(j).booking_id+members.get(j).member_id);
                                        List<VenueGroup> venueGroups = CardDao.queryGroupId(members.get(j).booking_id + members.get(j).member_id);
                                        if (venueGroups.size()==0){
                                            VenueGroup group=new VenueGroup(members.get(j).booking_id+members.get(j).member_id,members.get(j).booking_id,members.get(j).member_id,class_hour.get(i).id,"0");
                                            CardDao.insertGroup(group);
                                        }
                                    }
                                }
                            }
                            //删除
                            List<VenueGroup> venueGroups = CardDao.queryVenueGroup();
                            for (VenueGroup aboutUs:venueGroups){
                                boolean contains = VenueGroupId.contains(aboutUs.getId());
                                if (!contains){
                                    BaseApplication.getDaoInstant().getVenueGroupDao().deleteByKey(aboutUs.getId());
                                    BaseApplication.getDaoInstant().clear();
                                }
                            }
                            //删除
                            List<VenueKeyword> venueKeywords = CardDao.queryKey();
                            for (VenueKeyword aboutUs:venueKeywords){
                                boolean contains = KeywordsId.contains(aboutUs.getId());
                                if (!contains){
                                    BaseApplication.getDaoInstant().getVenueKeywordDao().deleteByKey(aboutUs.getId());
                                    BaseApplication.getDaoInstant().clear();
                                }
                            }
                            //删除
                            List<VenueEvaluate> venueEvaluates = CardDao.queryVenueEvaluate();
                            for (VenueEvaluate aboutUs:venueEvaluates){
                                boolean contains = VenueEvaluateId.contains(aboutUs.getId());
                                if (!contains){
                                    BaseApplication.getDaoInstant().getVenueEvaluateDao().deleteByKey(aboutUs.getId());
                                    BaseApplication.getDaoInstant().clear();
                                }
                            }
                        }
                    }catch (Exception e){
                        Log.e("ATG","场馆主页接口"+e.toString());
                        intent.setAction(Constant.ERR);
                        intent.putExtra(Constant.ERR,ClassCalendar.getStringtHour()+": 场馆主页接口解析错误"+e.getMessage());
                        sendBroadcast(intent);
                    }
                }else if(id==3){
                    try {
                        BookingId.clear();
                        //场馆记录
                        VenueBookingBean venueBookingBean = gson.fromJson(response, VenueBookingBean.class);
                        if (venueBookingBean.code==1){
                            List<VenueBookingBean.Data> data = venueBookingBean.data;
                            for (VenueBookingBean.Data king:data){
                                BookingId.add(king.id);
                                List<VenueBooKing> venueBooKings = CardDao.queryVenueBooKingId(king.id);
                                if (venueBooKings.size()!=0){
                                    if (!venueBooKings.get(0).getStart_time().equals(king.start_time)||!venueBooKings.get(0).getEnd_time().equals(king.end_time)||!venueBooKings.get(0).getDate().equals(king.date)
                                            ||!venueBooKings.get(0).getGroupname().equals(king.groupname)||!venueBooKings.get(0).getSignin().equals(king.signin)
                                            ||!venueBooKings.get(0).getTotal().equals(king.total)||!venueBooKings.get(0).getVenue_name().equals(king.venue_name)
                                            ||!venueBooKings.get(0).getBooker().equals(king.booker)||!venueBooKings.get(0).getManager_score().equals(king.manager_score)
                                            ||!venueBooKings.get(0).getUser_score().equals(king.user_score)){
                                        VenueBooKing booKing=new VenueBooKing(king.id,king.groupname,king.venue_name,king.start_time,king.end_time,king.date,king.total,king.signin,king.user_score,king.booker,king.manager_score,0,0);
                                        CardDao.insertVenueBooKing(booKing);
                                        intent.setAction(Constant.RECORD);

                                        sendBroadcast(intent);
                                    }
                                }else{
                                    VenueBooKing booKing=new VenueBooKing(king.id,king.groupname,king.venue_name,king.start_time,king.end_time,king.date,king.total,king.signin,king.user_score,king.booker,king.manager_score,0,0);
                                    CardDao.insertVenueBooKing(booKing);
                                    intent.setAction(Constant.RECORD);
                                    sendBroadcast(intent);
                                }
                            }
                        }
                        //删除
                        List<VenueBooKing> venueGroups = CardDao.queryVenueBooKing();
                        for (VenueBooKing aboutUs:venueGroups){
                            boolean contains = BookingId.contains(aboutUs.getId());
                            if (!contains){
                                BaseApplication.getDaoInstant().getVenueBooKingDao().deleteByKey(aboutUs.getId());
                                BaseApplication.getDaoInstant().clear();
                                intent.setAction(Constant.RECORD);
                                sendBroadcast(intent);
                            }
                        }
                    }catch (Exception e){
                        Log.e("ATG","场馆记录接口"+e.toString());
                        intent.setAction(Constant.ERR);
                        intent.putExtra(Constant.ERR,ClassCalendar.getStringtHour()+": 场馆记录接口解析错误"+e.getMessage());
                        sendBroadcast(intent);
                    }
                }
                }catch (Exception e){
                Log.e("ATG","主页接口"+e.toString());
                intent.setAction(Constant.ERR);
                intent.putExtra(Constant.ERR,ClassCalendar.getStringtHour()+": 主页接口接口解析错误"+e.toString());
                sendBroadcast(intent);
            }
        }
    }
}